-- api:/user_account/get_auth_user

local args = ngx.req.get_uri_args()
ngx.req.read_body()
--获取请求体body数据
local data,is_ok = common_util.get_data()
local device_info_arr = cjson_safe.decode(data)
local token = args.token
local user_id = nil
local type = args.type --gateway or device

if not common_util.check_args(1, token) then
    common_util.exit(200, 0, nil, "101", "token is missing")
end

local user_id = common_util.get_user_id_by_token(token)
if not user_id then
    common_util.exit(200, 0, nil, "105", "token is ....")
end

if not next(device_id_arr) then
    common_util.exit(200, 0, nil, "101", "device_id_arr is null")
end

local auth_user = common_util.luuid()
local passwd = ngx.md5(common_util.luuid())
local sql = "select account,passwd,expire_time from tb_emq_temp_user_info where user_id="..user_id
local data,bool = common_util.db_query_common(sql,common_config.db_user_info_addr)
if true ~= bool  then
    --connection:over()
    common_util.exit(200, 0, nil, "500", "mysql is error")
end

local update_sql
if next(data) then
   if tonumber(data[1].expire_time) > ngx.time() then
      auth_user = data[1].account
      passwd = data[1].passwd
      update_sql = "update tb_emq_temp_user_info set expire_time="..(ngx.time()+86400).." where user_id="..user_id
   else
      update_sql = "update tb_emq_temp_user_info set expire_time="..(ngx.time()+86400)..",account="..ngx.quote_sql_str(auth_user)
          ..",passwd="..ngx.quote_sql_str(passwd).." where user_id="..user_id
   end
else
    update_sql = "insert into tb_emq_temp_user_info set expire_time="..(ngx.time()+86400)..",account="..ngx.quote_sql_str(auth_user)
    ..",passwd="..ngx.quote_sql_str(passwd)..",user_id="..user_id..",create_time="..ngx.now()*1000
end

local data,bool = common_util.db_query_common(update_sql,common_config.db_user_info_addr)
if true ~= bool  then
    --connection:over()
    common_util.exit(200, 0, nil, "500", "mysql is error")
end

if not mqtt_util.add_emq_user(auth_user, passwd, 86400) then
    common_util.exit(200, 0, nil, "500", "add_emq_user is error")
end

local fair_arr = {}
local success_arr = {}
setmetatable(fair_arr, cjson.empty_array_mt)
setmetatable(success_arr, cjson.empty_array_mt)

for _, device_info in pairs(device_info_arr) do
    local auth_obj = mqtt_util.get_device_authority(device_info.device_id, device_info.gateway_id, user_id) or {}
    if next(auth_obj) then
        if true == auth_obj.is_view then
            if not mqtt_util.add_user_acl(auth_user, device_info.device_id .. "/" .. common_config.mqtt_config.view_topic, "sub") then
                table.insert(fair_arr, device_info.device_id)
            end
            table.insert(success_arr, device_info.device_id)
        else
            table.insert(fair_arr, device_info.device_id)
        end
    else
        table.insert(fair_arr, device_info.device_id)
    end
end

local res_json = {
    ["fair_arr"] = fair_arr,
    ["success_arr"] = success_arr,
    ["auth_user"] = auth_user,
    ["passwd"] = passwd,
}

common_util.exit(200, 1, res_json, "", "success")